using AdmissionQA.Application.Common;
using AdmissionQA.Application.ServiceInterfaces;
using AdmissionQA.Application.Services;
using AdmissionQA.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;

namespace AdmissionQA.Application;

public static class ServiceCollectionExtension
{
    public static void AddApplication(
        this IServiceCollection services,
        IConfiguration configuration
    )
    {
        services.AddInfrastructure(configuration);
        // 注册文本向量化服务（全局唯一，初始化时批量获取所有文本）
        using (var scope = services.BuildServiceProvider().CreateScope())
        {
            var db = scope.ServiceProvider.GetRequiredService<AdmissionQA.Infrastructure.Date.AdmissionQADbContext>();
            var corpus = new List<string>();
            corpus.AddRange(db.AdmissionPolicies.Select(x => x.AdmissionPolicyName + " " + x.AdmissionPolicyContent).ToList());
            corpus.AddRange(db.CampusLives.Select(x => x.Category).ToList());
            corpus.AddRange(db.Issues.Select(x => x.Question + " " + x.Answer).ToList());
            corpus.AddRange(db.MajorInfos.Select(x => x.MajorName + " " + x.CareerProspects).ToList());
            corpus.AddRange(db.ScholarshipInfos.Select(x => x.ScholarshipName + " " + x.Requirements).ToList());
            corpus.AddRange(db.TuitionFees.Select(x => x.Description ?? string.Empty).ToList());
            services.AddSingleton<ITextEmbeddingService>(new TextEmbeddingService(corpus));
        }
        // 注册服务接口和实现
        services.AddScoped<IAdmissionPolicyServices, AdmissionPolicyServices>();
        services.AddScoped<IIssueServices, IssueServices>();
        services.AddScoped<IMajorInfoServices, MajorInfoServices>();
        services.AddScoped<ICampusLifeServices, CampusLifeServices>();
        services.AddScoped<IScholarshipInfoServices, ScholarshipInfoServices>();
        services.AddScoped<ITuitionFeeServices, TuitionFeeServices>();
        services.AddScoped<IQAHistoryServices, QAHistoryServices>();
        
        // 注册DeepSeek服务
        services.AddHttpClient<IDeepSeekService, DeepSeekService>();

    }
}
